# !/usr/bin/env python
# -*- coding: utf-8 -*-
# @File  : 癌症分类案例.py
# @Author: dongguangwen
# @Date  : 2025-02-06 17:27
# 0.导包
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score


# 1.加载数据
data = pd.read_csv('./data/breast-cancer-wisconsin.csv')
print(data.info())

# 2.数据处理
# 2.1 缺失值
data = data.replace(to_replace='?', value=np.nan)
# print(data.info())
data = data.dropna()

# 2.2 获取特征和目标值
X = data.iloc[:, :-1]
y = data['Class']

# 2.3 数据划分
x_train, x_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=22)

# 3.特征工程(标准化)
pre = StandardScaler()
x_train = pre.fit_transform(x_train)
x_test = pre.transform(x_test)

# 4.模型训练
model = LogisticRegression()
model.fit(x_train, y_train)

# 5.模型预测和评估
y_pred = model.predict(x_test)
print(y_pred)

score1 = model.score(x_test, y_test)
print(score1)
score2 = accuracy_score(y_test, y_pred)
print(score2)

"""
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 699 entries, 0 to 698
Data columns (total 11 columns):
 #   Column                       Non-Null Count  Dtype 
---  ------                       --------------  ----- 
 0   Sample code number           699 non-null    int64 
 1   Clump Thickness              699 non-null    int64 
 2   Uniformity of Cell Size      699 non-null    int64 
 3   Uniformity of Cell Shape     699 non-null    int64 
 4   Marginal Adhesion            699 non-null    int64 
 5   Single Epithelial Cell Size  699 non-null    int64 
 6   Bare Nuclei                  699 non-null    object
 7   Bland Chromatin              699 non-null    int64 
 8   Normal Nucleoli              699 non-null    int64 
 9   Mitoses                      699 non-null    int64 
 10  Class                        699 non-null    int64 
dtypes: int64(10), object(1)
memory usage: 60.2+ KB
None
[2 4 4 2 2 2 2 2 2 2 2 2 2 4 2 2 4 4 4 2 4 2 4 4 4 2 4 2 2 2 2 2 4 2 2 2 4
 2 2 2 2 4 2 4 4 4 4 2 4 4 2 2 2 2 2 4 2 2 2 2 4 4 4 4 2 4 2 2 4 2 2 2 2 4
 2 2 2 2 2 2 4 4 4 2 4 4 4 4 2 2 2 4 2 4 2 2 2 2 2 2 4 2 2 4 2 2 4 2 4 4 2
 2 2 2 4 2 2 2 2 2 2 4 2 4 2 2 2 4 2 4 2 2 2 4 2 2 2]
0.9854014598540146
0.9854014598540146
"""